<template>
  <el-card class="sku-list">
    <el-table border :data="list">
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        align="center"></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片" width="120px" align="center">
        <template v-slot="{ row }">
          <el-image :src="row.skuDefaultImg"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="重量(KG)" prop="weight"></el-table-column>
      <el-table-column label="价格(元)" prop="price"></el-table-column>
      <el-table-column label="操作" width="240px" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.isSale === 1"
            size="small"
            type="success"
            :icon="Bottom"
            title="下架"
            @click="topOrbottomSale(row)"></el-button>
          <el-button
            v-else
            size="small"
            type="info"
            title="上架"
            :icon="Top"
            @click="topOrbottomSale(row)"></el-button>

          <el-button
            size="small"
            title="编辑"
            type="primary"
            :icon="Edit"></el-button>
          <el-button
            size="small"
            type="warning"
            title="查看详情"
            :icon="Warning"
            @click="showSkuInfo(row.id)"></el-button>
          <el-popconfirm
            :title="`确定是否删除${row.skuName}吗?`"
            @confirm="deleteSku(row.id)">
            <template #reference>
              <el-button
                size="small"
                type="danger"
                title="删除"
                :icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px"
      v-model:current-page="SkuInfo.page"
      v-model:page-size="SkuInfo.limit"
      :total="SkuInfo.total"
      :page-sizes="[10, 20, 30]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @current-change="getList"
      @size-change="getList" />

    <el-drawer
      v-model="skuInfoData.isShowInfo"
      direction="rtl"
      size="50%"
      :with-header="false">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfoData.skuInfo?.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfoData.skuInfo?.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfoData.skuInfo?.price }}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="18">
          <div
            v-if="
              skuInfoData.skuInfo?.skuAttrValueList &&
              skuInfoData.skuInfo?.skuAttrValueList.length > 0
            ">
            <el-tag
              type="success"
              style="margin-right: 5px"
              v-for="value in skuInfoData.skuInfo?.skuAttrValueList"
              :key="value.id">
              {{ value.attrName + " / " + value.valueName }}
            </el-tag>
          </div>
          <div v-else>无</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">销售属性</el-col>
        <el-col :span="18">
          <el-tag
            type="success"
            style="margin-right: 5px"
            v-for="value in skuInfoData.skuInfo?.skuSaleAttrValueList"
            :key="value.id"
            v-if="
              skuInfoData.skuInfo?.skuSaleAttrValueList &&
              skuInfoData.skuInfo?.skuSaleAttrValueList.length > 0
            ">
            {{ value.saleAttrName + " / " + value.saleAttrValueName }}
          </el-tag>
          <div v-else>无</div>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel
            class="sku-carousel"
            trigger="click"
            height="400px"
            style="width: 400px">
            <el-carousel-item
              v-for="item in skuInfoData.skuInfo?.skuImageList"
              :key="item.id">
              <img
                :src="item.imgUrl"
                alt=""
                style="width: 100%; height: 100%" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </el-card>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Sku",
});
</script>

<script setup lang="ts">
import type {
  SkuImageModel,
  SkuListModel,
  SkuModel,
} from "@/api/product/model/skuModel";
import {
  cancelSaleApi,
  deleteSkuApi,
  getSkuInfoApi,
  getSkuPageListApi,
  onSaleApi,
} from "@/api/product/sku";
import { Edit, Warning, Delete, Top, Bottom } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

import { ref, reactive, onMounted, computed } from "vue";

const SkuInfo = reactive({
  page: 1,
  limit: 10,
  total: 0,
});

// 获取分页列表的请求
const getList = async () => {
  const result = await getSkuPageListApi(SkuInfo.page, SkuInfo.limit);
  console.log(result);
  list.value = result.records;
  SkuInfo.total = result.total;
};

// 上架 或者下架
const topOrbottomSale = async (row: SkuModel) => {
  if (row.isSale === 1) {
    await cancelSaleApi(row.id!);
    ElMessage.success("下架成功");
    row.isSale = 0;
  } else if (row.isSale === 0) {
    await onSaleApi(row.id!);
    ElMessage.success("上架成功");
    row.isSale = 1;
  } else {
    return;
  }
};

// 在组件挂载的时候调用请求列表
onMounted(() => {
  getList();
});
const list = ref<SkuListModel>();

// sku详情相关数据
const skuInfoData = reactive<{
  isShowInfo: boolean; // 是否显示详情
  skuInfo?: SkuModel; // sku详情
}>({
  isShowInfo: false,
  skuInfo: undefined,
});
// 显示sku详情
const showSkuInfo = async (id: number) => {
  // 发请求
  skuInfoData.skuInfo = await getSkuInfoApi(id);
  // 显示
  skuInfoData.isShowInfo = true;
};

// 删除
const deleteSku = async (id: number) => {
  await deleteSkuApi(id);
  ElMessage.success("删除成功");
  getList();
};
</script>

<style scoped lang="scss">
.sku-list {
  .el-row {
    // Row组件的根标签
    margin: 10px 0;
    .el-col-5 {
      // Col组件的根标签
      text-align: right;
      font-size: 18px;
      margin-right: 20px;
    }
  }

  :deep(.el-carousel__indicator) {
    // li / button   // Carousel的子标签
    button {
      background-color: pink;
    }
    &.is-active {
      // &称为父级引用
      button {
        background-color: hotpink;
      }
    }
  }
}
</style>
